package cn.stylefeng.guns.modular.controller;


import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.Device;
import cn.stylefeng.guns.modular.service.IDeviceService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 设备表 前端控制器
 * </p>
 *
 * @author XiangTianGe
 * @since 2020-04-13
 */
@Controller
@RequestMapping("device")
public class DeviceController extends BaseController {

    private static String PREFIX = "/device/";

    @Autowired
    private IDeviceService deviceService;

    /**
     * 跳转设备首页
     */
    @RequestMapping
    public String index() {
        return PREFIX + "device.html";
    }

    /**
     * 跳转设备新增
     */
    @RequestMapping("add")
    public ModelAndView add() {
        ModelAndView add = deviceService.add();
        add.setViewName(PREFIX + "device_add.html");
        return add;
    }

    /**
     * 跳转设备修改
     */
    @RequestMapping("edit/{id}")
    public ModelAndView edit(@PathVariable Long deviceId) {
        ModelAndView edit = deviceService.edit(deviceId);
        edit.setViewName(PREFIX + "device_edit.html");
        return deviceService.edit(deviceId);
    }

    /**
     * 设备列表
     */
    @ResponseBody
    @RequestMapping("list")
    public LayuiPageInfo list(String keyword, String deviceType, Integer qrcode, String timeLimit) {

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //拼接查询条件
        String beginTime = null;
        String endTime = null;

        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        //根据条件查询日志
        return deviceService.deviceList(keyword, deviceType, qrcode, beginTime, endTime, page);
    }

    /**
     * 新增设备
     */
    @ResponseBody
    @RequestMapping("device-add")
    public ResponseData deviceAdd(@RequestBody Device device) {
        ResponseData responseData = deviceService.deviceAdd(device);
        return responseData;
    }


    /**
     * 修改设备
     */
    @ResponseBody
    @RequestMapping("device-edit")
    public ResponseData deviceEdit(@RequestBody Device device) {
        deviceService.deviceEdit(device);
        return SUCCESS_TIP;
    }

    /**
     * 修改设备
     */
    @ResponseBody
    @RequestMapping("delete/{deviceId}")
    public ResponseData delete(@PathVariable Long deviceId) {
        deviceService.delete(deviceId);
        return SUCCESS_TIP;
    }

    /**
     * 查看
     */
    @RequestMapping("details/{deviceId}")
    public ModelAndView queryDetails(@PathVariable Long deviceId) {

        ModelAndView modelAndView = deviceService.queryDetails(deviceId);
        modelAndView.setViewName(PREFIX + "device_details.html");
        return modelAndView;
    }

    /**
     * 生成二维码
     */
    @ResponseBody
    @RequestMapping("generate-qrcode/{deviceId}")
    public ResponseData generateCode(@PathVariable Long deviceId) {
        return deviceService.generateCode(deviceId);
    }
}

